package opc.read.OpcQie;

import lombok.SneakyThrows;
import opc.read.utils.ItemKey2Convert;
import opc.read.utils.MyCopyOnWriteMap;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.da.AccessBase;
import org.openscada.opc.lib.da.DataCallback;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.ItemState;

import java.util.Map;

/**
 * @Description: 线程执行采集任务，因为传值是复制传值而不是引用传值，所以代码错误
 * @author: liyuan
 * @data 2021-01-22 11:40
 *
 */
@Deprecated
public class SingThread extends Thread{
    /** 信号源 **/
    private Map<String, Map<String, Item>> itemSource;
    /** 存放着结果 **/
    private MyCopyOnWriteMap<String, Object> result;
    private AccessBase access;
    //itemSource,result,access
    public SingThread(Map<String, Map<String, Item>> itemSource, MyCopyOnWriteMap<String, Object> result ,AccessBase access){
        this.itemSource=itemSource;
        this.result=result;
        this.access=access;
    }
    public SingThread(String name,Map<String, Map<String, Item>> itemSource, MyCopyOnWriteMap<String, Object> result ,AccessBase access){
        super(name);
        new SingThread(itemSource,  result , access);
    }
/***
 SingThread t1=new SingThread("t1",itemSourceZu.get(0),result,access);
 SingThread t2=new SingThread("t2",itemSourceZu.get(1),result,access);
 SingThread t3=new SingThread("t3",itemSourceZu.get(2),result,access);
 SingThread t4=new SingThread("t4",itemSourceZu.get(3),result,access);
 SingThread t5=new SingThread("t5",itemSourceZu.get(4),result,access);
 变量副本导致result是旧数据，
 * */
    @SneakyThrows
    @Override
    public void run() {
        for (final String groupName : itemSource.keySet()) {
            Map<String, Item> itemMap = itemSource.get(groupName);
            //组内部的读取信号
            for (Map.Entry<String, Item> map : itemMap.entrySet()) {
                if (map.getValue() instanceof Item) {
                    //读取单个数据的代码
                    access.addItem(map.getValue().getId(), new DataCallback() {
                        /*** DataCallback()是信号值变化的事件回调函数  从结果来看，没有改变的信号也在输出  **/
                        @Override
                        public void changed(Item item, ItemState state) {
                            String tmpKey = item.getId().split("\\.")[2];
                            String itemKey = ItemKey2Convert.in2Out(tmpKey); /** 从opcserver出来的值转换一下 **/
                            Map<String, Object> itemMap = (Map<String, Object>) result.get(groupName);
                            try {
                                if (itemMap.containsKey(itemKey)) {
                                    itemMap.put(itemKey, state.getValue().getObject());  // 去掉xianhe.设备 1., 只留下需要的信号名
//                                        System.out.println("itemKey: " + itemKey + ", state.getValue().getObject(): " + state.getValue().getObject());
                                } else {
                                    StringBuilder builder=new StringBuilder(getName());
                                    builder.append("，键值对出错：");
                                    builder.append(tmpKey);
                                    builder.append(",");
                                    builder.append(itemKey);
                                    throw new Exception(builder.toString());
                                }

                            } catch (JIException e) {
                                e.printStackTrace();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    });
                }
            }
        }
        access.bind();
    }
}
